package com.huabo.contract.service.impl;


import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.TblContractInformation;
import com.huabo.contract.oracle.entity.TblCyhwUnit;
import com.huabo.contract.oracle.mapper.TblContractInformationMapper;
import com.huabo.contract.oracle.mapper.TblCyhwUnitMapper;
import com.huabo.contract.service.TblContractInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblContractInformationServiceImpl implements TblContractInformationService {

    @Resource
    private TblCyhwUnitMapper tblCyhwUnitMapper;

    @Resource
    private TblContractInformationMapper tblContractInformationMapper;


    @Override
    public Map<String, Object> saveContractInfoMation(Integer contractId,TblContractInformation information) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblCyhwUnit unit = this.tblCyhwUnitMapper.findContractById(contractId);
                information.setProjectid(unit.getContractid());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            if (information.getInfostartdate() != null && !"".equals(information.getInfostartdate())) {
//                information.setInfostartdate(sdf.parse(information.getInfostartdate()));
//            }
//            if (information.getInfostartdate() != null && !"".equals(information.getInfostartdate())) {
//                information.setInfoenddate(sdf.parse(information.getInfostartdate()));
//            }
                if (information.getInfoid() != null) {
                    this.tblContractInformationMapper.updateContractInfoMation(information);
                } else {
                    this.tblContractInformationMapper.saveContractInfoMation(information);
                }

            } catch (Exception e) {
                resultMap.put("code", "-1");
                resultMap.put("msg", "保存失败");
                return resultMap;
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "保存成功");
            resultMap.put("data", information);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblCyhwUnit unit = this.tblCyhwUnitMapper.findContractById(contractId);
                information.setProjectid(unit.getContractid());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            if (information.getInfostartdate() != null && !"".equals(information.getInfostartdate())) {
//                information.setInfostartdate(sdf.parse(information.getInfostartdate()));
//            }
//            if (information.getInfostartdate() != null && !"".equals(information.getInfostartdate())) {
//                information.setInfoenddate(sdf.parse(information.getInfostartdate()));
//            }
                if (information.getInfoid() != null) {
                    this.tblContractInformationMapper.updateContractInfoMation(information);
                } else {
                    this.tblContractInformationMapper.saveContractInfoMation(information);
                }

            } catch (Exception e) {
                resultMap.put("code", "-1");
                resultMap.put("msg", "保存失败");
                return resultMap;
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "保存成功");
            resultMap.put("data", information);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> removeContractInfoMation(Integer infoId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                this.tblContractInformationMapper.removeContractInfoMation(infoId);
            } catch (Exception e) {
                resultMap.put("code", "-1");
                resultMap.put("msg", "删除失败");
                return resultMap;
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "删除成功");
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                this.tblContractInformationMapper.removeContractInfoMation(infoId);
            } catch (Exception e) {
                resultMap.put("code", "-1");
                resultMap.put("msg", "删除失败");
                return resultMap;
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "删除成功");
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findInformationListById(Integer contractId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                List<TblContractInformation> informationList = this.tblContractInformationMapper.findInformationListById(new BigDecimal(contractId));
                resultMap.put("code", "1");
                resultMap.put("msg", "成功");
                resultMap.put("data", informationList);
            } catch (Exception e) {
                resultMap.put("code", "0");
                resultMap.put("msg", "失败");
                return resultMap;
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                List<TblContractInformation> informationList = this.tblContractInformationMapper.findInformationListById(new BigDecimal(contractId));
                resultMap.put("code", "1");
                resultMap.put("msg", "成功");
                resultMap.put("data", informationList);
            } catch (Exception e) {
                resultMap.put("code", "0");
                resultMap.put("msg", "失败");
                return resultMap;
            }
            return resultMap;
        }
    }

}
